using Microsoft.Phone.Controls.Properties;
using System;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace Microsoft.Phone.Controls
{
	[TemplatePart(Name = "Presenter", Type = typeof(ItemsPresenter)), TemplatePart(Name = "ExpanderPanel", Type = typeof(Grid)), TemplatePart(Name = "CollapsedToExpandedKeyFrame", Type = typeof(EasingDoubleKeyFrame)), TemplatePart(Name = "ExpandedToCollapsedKeyFrame", Type = typeof(EasingDoubleKeyFrame)), TemplatePart(Name = "ExpandedStateAnimation", Type = typeof(DoubleAnimation)), TemplateVisualState(Name = "NonExpandable", GroupName = "ExpandabilityStates"), TemplateVisualState(Name = "Collapsed", GroupName = "ExpansionStates"), TemplateVisualState(Name = "Expanded", GroupName = "ExpansionStates"), TemplateVisualState(Name = "Expandable", GroupName = "ExpandabilityStates")]
	public class ExpanderView : HeaderedItemsControl
	{
		public const string ExpansionStates = "ExpansionStates";
		public const string ExpandabilityStates = "ExpandabilityStates";
		public const string CollapsedState = "Collapsed";
		public const string ExpandedState = "Expanded";
		public const string ExpandableState = "Expandable";
		public const string NonExpandableState = "NonExpandable";
		private const string Presenter = "Presenter";
		private const string ExpanderPanel = "ExpanderPanel";
		private const string ExpandedStateAnimation = "ExpandedStateAnimation";
		private const string CollapsedToExpandedKeyFrame = "CollapsedToExpandedKeyFrame";
		private const string ExpandedToCollapsedKeyFrame = "ExpandedToCollapsedKeyFrame";
		private const int KeyTimeStep = 35;
		private const int InitialKeyTime = 225;
		private const int FinalKeyTime = 250;
		private ItemsPresenter _presenter;
		private Canvas _itemsCanvas;
		private Grid _expanderPanel;
		private DoubleAnimation _expandedStateAnimation;
		private EasingDoubleKeyFrame _collapsedToExpandedFrame;
		private EasingDoubleKeyFrame _expandedToCollapsedFrame;
		public static readonly DependencyProperty ExpanderProperty = DependencyProperty.Register("Expander", typeof(object), typeof(ExpanderView), new PropertyMetadata(null, new PropertyChangedCallback(ExpanderView.OnExpanderPropertyChanged)));
		public static readonly DependencyProperty ExpanderTemplateProperty = DependencyProperty.Register("ExpanderTemplate", typeof(DataTemplate), typeof(ExpanderView), new PropertyMetadata(null, new PropertyChangedCallback(ExpanderView.OnExpanderTemplatePropertyChanged)));
		public static readonly DependencyProperty NonExpandableHeaderProperty = DependencyProperty.Register("NonExpandableHeader", typeof(object), typeof(ExpanderView), new PropertyMetadata(null, new PropertyChangedCallback(ExpanderView.OnNonExpandableHeaderPropertyChanged)));
		public static readonly DependencyProperty NonExpandableHeaderTemplateProperty = DependencyProperty.Register("NonExpandableHeaderTemplate", typeof(DataTemplate), typeof(ExpanderView), new PropertyMetadata(null, new PropertyChangedCallback(ExpanderView.OnNonExpandableHeaderTemplatePropertyChanged)));
		public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(bool), typeof(ExpanderView), new PropertyMetadata(false, new PropertyChangedCallback(ExpanderView.OnIsExpandedPropertyChanged)));
		public static readonly DependencyProperty HasItemsProperty = DependencyProperty.Register("HasItems", typeof(bool), typeof(ExpanderView), new PropertyMetadata(false, null));
		public static readonly DependencyProperty IsNonExpandableProperty = DependencyProperty.Register("IsNonExpandable", typeof(bool), typeof(ExpanderView), new PropertyMetadata(false, new PropertyChangedCallback(ExpanderView.OnIsNonExpandablePropertyChanged)));
		public event RoutedEventHandler Expanded;
		public event RoutedEventHandler Collapsed;
		public object Expander
		{
			get
			{
				return base.GetValue(ExpanderView.ExpanderProperty);
			}
			set
			{
				base.SetValue(ExpanderView.ExpanderProperty, value);
			}
		}
		public DataTemplate ExpanderTemplate
		{
			get
			{
				return (DataTemplate)base.GetValue(ExpanderView.ExpanderTemplateProperty);
			}
			set
			{
				base.SetValue(ExpanderView.ExpanderTemplateProperty, value);
			}
		}
		public object NonExpandableHeader
		{
			get
			{
				return base.GetValue(ExpanderView.NonExpandableHeaderProperty);
			}
			set
			{
				base.SetValue(ExpanderView.NonExpandableHeaderProperty, value);
			}
		}
		public DataTemplate NonExpandableHeaderTemplate
		{
			get
			{
				return (DataTemplate)base.GetValue(ExpanderView.NonExpandableHeaderTemplateProperty);
			}
			set
			{
				base.SetValue(ExpanderView.NonExpandableHeaderTemplateProperty, value);
			}
		}
		public bool IsExpanded
		{
			get
			{
				return (bool)base.GetValue(ExpanderView.IsExpandedProperty);
			}
			set
			{
				if (this.IsNonExpandable)
				{
					throw new InvalidOperationException(Resources.InvalidExpanderViewOperation);
				}
				base.SetValue(ExpanderView.IsExpandedProperty, value);
			}
		}
		public bool HasItems
		{
			get
			{
				return (bool)base.GetValue(ExpanderView.HasItemsProperty);
			}
			set
			{
				base.SetValue(ExpanderView.HasItemsProperty, value);
			}
		}
		public bool IsNonExpandable
		{
			get
			{
				return (bool)base.GetValue(ExpanderView.IsNonExpandableProperty);
			}
			set
			{
				base.SetValue(ExpanderView.IsNonExpandableProperty, value);
			}
		}
		private static void OnExpanderPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			ExpanderView expanderView = (ExpanderView)obj;
			expanderView.OnExpanderChanged(e.get_OldValue(), e.get_NewValue());
		}
		private static void OnExpanderTemplatePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			ExpanderView expanderView = (ExpanderView)obj;
			expanderView.OnExpanderTemplateChanged((DataTemplate)e.get_OldValue(), (DataTemplate)e.get_NewValue());
		}
		private static void OnNonExpandableHeaderPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			ExpanderView expanderView = (ExpanderView)obj;
			expanderView.OnNonExpandableHeaderChanged(e.get_OldValue(), e.get_NewValue());
		}
		private static void OnNonExpandableHeaderTemplatePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			ExpanderView expanderView = (ExpanderView)obj;
			expanderView.OnNonExpandableHeaderTemplateChanged((DataTemplate)e.get_OldValue(), (DataTemplate)e.get_NewValue());
		}
		private static void OnIsExpandedPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			ExpanderView expanderView = (ExpanderView)obj;
			RoutedEventArgs e2 = new RoutedEventArgs();
			if ((bool)e.get_NewValue())
			{
				expanderView.OnExpanded(e2);
			}
			else
			{
				expanderView.OnCollapsed(e2);
			}
			expanderView.UpdateVisualState(true);
		}
		private static void OnIsNonExpandablePropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			ExpanderView expanderView = (ExpanderView)obj;
			if ((bool)e.get_NewValue() && expanderView.IsExpanded)
			{
				expanderView.IsExpanded = false;
			}
			expanderView.UpdateVisualState(true);
		}
		public override void OnApplyTemplate()
		{
			if (this._expanderPanel != null)
			{
				this._expanderPanel.remove_Tap(new EventHandler<GestureEventArgs>(this.OnExpanderPanelTap));
			}
			base.OnApplyTemplate();
			this._expanderPanel = (base.GetTemplateChild("ExpanderPanel") as Grid);
			this._expandedStateAnimation = ((base.GetTemplateChild("Expanded") as VisualState).get_Storyboard().get_Children().get_Item(0) as DoubleAnimation);
			this._expandedToCollapsedFrame = (base.GetTemplateChild("ExpandedToCollapsedKeyFrame") as EasingDoubleKeyFrame);
			this._collapsedToExpandedFrame = (base.GetTemplateChild("CollapsedToExpandedKeyFrame") as EasingDoubleKeyFrame);
			this._itemsCanvas = (base.GetTemplateChild("ItemsCanvas") as Canvas);
			this._presenter = (base.GetTemplateChild("Presenter") as ItemsPresenter);
			this._presenter.add_SizeChanged(new SizeChangedEventHandler(this.OnPresenterSizeChanged));
			if (this._expanderPanel != null)
			{
				this._expanderPanel.add_Tap(new EventHandler<GestureEventArgs>(this.OnExpanderPanelTap));
			}
			this.UpdateVisualState(false);
		}
		public ExpanderView()
		{
			base.set_DefaultStyleKey(typeof(ExpanderView));
			base.add_SizeChanged(new SizeChangedEventHandler(this.OnSizeChanged));
		}
		private void OnSizeChanged(object sender, SizeChangedEventArgs e)
		{
			if (this._presenter != null)
			{
				UIElement uIElement = VisualTreeHelper.GetParent(this._presenter) as UIElement;
				while (!(uIElement is ExpanderView))
				{
					uIElement = (VisualTreeHelper.GetParent(uIElement) as UIElement);
				}
				GeneralTransform generalTransform = uIElement.TransformToVisual(this._presenter);
				Point point = generalTransform.Transform(new Point(0.0, 0.0));
				this._presenter.set_Width(uIElement.get_RenderSize().get_Width() + point.get_X());
			}
		}
		private void OnPresenterSizeChanged(object sender, SizeChangedEventArgs e)
		{
			if (this._itemsCanvas != null && this._presenter != null && this.IsExpanded)
			{
				this._itemsCanvas.set_Height(this._presenter.get_DesiredSize().get_Height());
			}
		}
		internal virtual void UpdateVisualState(bool useTransitions)
		{
			if (this._presenter != null)
			{
				if (this._expandedStateAnimation != null)
				{
					this._expandedStateAnimation.set_To(new double?(this._presenter.get_DesiredSize().get_Height()));
				}
				if (this._collapsedToExpandedFrame != null)
				{
					this._collapsedToExpandedFrame.set_Value(this._presenter.get_DesiredSize().get_Height());
				}
				if (this._expandedToCollapsedFrame != null)
				{
					this._expandedToCollapsedFrame.set_Value(this._presenter.get_DesiredSize().get_Height());
				}
			}
			string text;
			if (this.IsExpanded)
			{
				text = "Expanded";
				if (useTransitions)
				{
					this.AnimateContainerDropDown();
				}
			}
			else
			{
				text = "Collapsed";
			}
			VisualStateManager.GoToState(this, text, useTransitions);
			string text2;
			if (this.IsNonExpandable)
			{
				text2 = "NonExpandable";
			}
			else
			{
				text2 = "Expandable";
			}
			VisualStateManager.GoToState(this, text2, useTransitions);
		}
		private void RaiseEvent(RoutedEventHandler handler, RoutedEventArgs args)
		{
			if (handler != null)
			{
				handler.Invoke(this, args);
			}
		}
		internal void AnimateContainerDropDown()
		{
			for (int i = 0; i < base.get_Items().get_Count(); i++)
			{
				FrameworkElement frameworkElement = base.get_ItemContainerGenerator().ContainerFromIndex(i) as FrameworkElement;
				if (frameworkElement == null)
				{
					return;
				}
				Storyboard storyboard = new Storyboard();
				QuadraticEase quadraticEase = new QuadraticEase();
				quadraticEase.set_EasingMode(0);
				IEasingFunction easingFunction = quadraticEase;
				int num = 225 + 35 * i;
				int num2 = 250 + 35 * i;
				TranslateTransform translateTransform = new TranslateTransform();
				frameworkElement.set_RenderTransform(translateTransform);
				DoubleAnimationUsingKeyFrames doubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
				EasingDoubleKeyFrame easingDoubleKeyFrame = new EasingDoubleKeyFrame();
				easingDoubleKeyFrame.set_EasingFunction(easingFunction);
				easingDoubleKeyFrame.set_KeyTime(TimeSpan.FromMilliseconds(0.0));
				easingDoubleKeyFrame.set_Value(-150.0);
				EasingDoubleKeyFrame easingDoubleKeyFrame2 = new EasingDoubleKeyFrame();
				easingDoubleKeyFrame2.set_EasingFunction(easingFunction);
				easingDoubleKeyFrame2.set_KeyTime(TimeSpan.FromMilliseconds((double)num));
				easingDoubleKeyFrame2.set_Value(0.0);
				EasingDoubleKeyFrame easingDoubleKeyFrame3 = new EasingDoubleKeyFrame();
				easingDoubleKeyFrame3.set_EasingFunction(easingFunction);
				easingDoubleKeyFrame3.set_KeyTime(TimeSpan.FromMilliseconds((double)num2));
				easingDoubleKeyFrame3.set_Value(0.0);
				doubleAnimationUsingKeyFrames.get_KeyFrames().Add(easingDoubleKeyFrame);
				doubleAnimationUsingKeyFrames.get_KeyFrames().Add(easingDoubleKeyFrame2);
				doubleAnimationUsingKeyFrames.get_KeyFrames().Add(easingDoubleKeyFrame3);
				Storyboard.SetTarget(doubleAnimationUsingKeyFrames, translateTransform);
				Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames, new PropertyPath(TranslateTransform.YProperty));
				storyboard.get_Children().Add(doubleAnimationUsingKeyFrames);
				DoubleAnimationUsingKeyFrames doubleAnimationUsingKeyFrames2 = new DoubleAnimationUsingKeyFrames();
				EasingDoubleKeyFrame easingDoubleKeyFrame4 = new EasingDoubleKeyFrame();
				easingDoubleKeyFrame4.set_EasingFunction(easingFunction);
				easingDoubleKeyFrame4.set_KeyTime(TimeSpan.FromMilliseconds(0.0));
				easingDoubleKeyFrame4.set_Value(0.0);
				EasingDoubleKeyFrame easingDoubleKeyFrame5 = new EasingDoubleKeyFrame();
				easingDoubleKeyFrame5.set_EasingFunction(easingFunction);
				easingDoubleKeyFrame5.set_KeyTime(TimeSpan.FromMilliseconds((double)(num - 150)));
				easingDoubleKeyFrame5.set_Value(0.0);
				EasingDoubleKeyFrame easingDoubleKeyFrame6 = new EasingDoubleKeyFrame();
				easingDoubleKeyFrame6.set_EasingFunction(easingFunction);
				easingDoubleKeyFrame6.set_KeyTime(TimeSpan.FromMilliseconds((double)num2));
				easingDoubleKeyFrame6.set_Value(1.0);
				doubleAnimationUsingKeyFrames2.get_KeyFrames().Add(easingDoubleKeyFrame4);
				doubleAnimationUsingKeyFrames2.get_KeyFrames().Add(easingDoubleKeyFrame5);
				doubleAnimationUsingKeyFrames2.get_KeyFrames().Add(easingDoubleKeyFrame6);
				Storyboard.SetTarget(doubleAnimationUsingKeyFrames2, frameworkElement);
				Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames2, new PropertyPath(UIElement.OpacityProperty));
				storyboard.get_Children().Add(doubleAnimationUsingKeyFrames2);
				storyboard.Begin();
			}
		}
		protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
		{
			base.OnItemsChanged(e);
			this.HasItems = (base.get_Items().get_Count() > 0);
		}
		private void OnExpanderPanelTap(object sender, GestureEventArgs e)
		{
			if (!this.IsNonExpandable)
			{
				this.IsExpanded = !this.IsExpanded;
			}
		}
		protected virtual void OnExpanderChanged(object oldExpander, object newExpander)
		{
		}
		protected virtual void OnExpanderTemplateChanged(DataTemplate oldTemplate, DataTemplate newTemplate)
		{
		}
		protected virtual void OnNonExpandableHeaderChanged(object oldHeader, object newHeader)
		{
		}
		protected virtual void OnNonExpandableHeaderTemplateChanged(DataTemplate oldTemplate, DataTemplate newTemplate)
		{
		}
		protected virtual void OnExpanded(RoutedEventArgs e)
		{
			this.RaiseEvent(this.Expanded, e);
		}
		protected virtual void OnCollapsed(RoutedEventArgs e)
		{
			this.RaiseEvent(this.Collapsed, e);
		}
	}
}
